import {h} from "vue";
import {NButton, NSpace} from "naive-ui";

export const getOption = (types=[],
                   clickBack={},disabledButtons={},columnOption={},
                   buttonStyle={
                       updateStyle:{size: "tiny",type: 'primary',round: true,label:'修改'},
                       deleteStyle:{size: "tiny",type: 'error',round: true,label:'删除'},
                       showStyle:{size: "tiny",type: 'primary',round: true,label:'查看'},
                   })=>{
    const {updateClick,deleteClick,showClick} = clickBack;
    const {disabledDelete} = disabledButtons;
    const {updateStyle,deleteStyle,showStyle} = buttonStyle;
    const option = {
        title: '操作',
        key: 'option',
        width: 200,
        fixed: 'right',
        ...columnOption,
        render: (row, index) => {
            const buttonMap = {
                update:h(NButton, {
                        size: updateStyle?.size || "tiny",
                        type: updateStyle?.type || 'primary',
                        round: updateStyle?.round ,
                        onClick: () => {
                            updateClick && updateClick(row)
                        }
                    },
                    {default: () => updateStyle?.label || '修改'}
                ),
                delete:h(NButton, {
                        size: deleteStyle?.size || "tiny",
                        type: deleteStyle?.type || 'error',
                        round: deleteStyle?.round ,
                        onClick: () => {
                            deleteClick && deleteClick(row)
                        }
                    }, {default: () =>deleteClick?.label ||  "删除"}
                ),
                show:h(NButton, {
                        size: showStyle?.size || "tiny",
                        type: showStyle?.type || 'primary',
                        round: showStyle?.round,
                        onClick: () => {
                            showClick && showClick(row);
                        }
                    },
                    {default: () =>showClick?.label ||  '查看'}
                ),
            }
            const buttons = []
            if (types && types.length > 0){
                for (let type of types) {
                    if (type === 'delete' && disabledDelete && disabledDelete(row)){
                        break;
                    }
                    buttonMap[type] && buttons.push(buttonMap[type])
                }
            }
            return h(NSpace, null, () => buttons)
        }
    }

    return option;
}

export const getSelection = (disabledSelect)=> {
    return {
        type: "selection",
        disabled(row) {
            return disabledSelect && disabledSelect(row);
        }
    }
}

